package com.hgm.travelapp.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.hgm.travelapp.R
import com.hgm.travelapp.bean.Hot
import com.hgm.travelapp.bean.HotData
import com.hgm.travelapp.databinding.ActivitySearchBinding
import com.hgm.travelapp.network.ApiService
import com.hgm.travelapp.network.RetrofitClient
import retrofit2.Call
import retrofit2.Response

class SearchActivity : AppCompatActivity() {

    private val mBinding: ActivitySearchBinding by lazy {
        ActivitySearchBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //隐藏状态栏
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(mBinding.root)

        initHotList()
        initEvent()
    }

    private fun initEvent() {
        //监听搜索框
        mBinding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener,
            android.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                Toast.makeText(this@SearchActivity, "点击搜索按钮时触发", Toast.LENGTH_SHORT).show()
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //搜索不到隐藏热门商品
                if (newText == "找不到") {
                    mBinding.apply {
                        cardViewHot.visibility = View.GONE
                        recyclerSearch.visibility=View.GONE
                        llNone.visibility = View.VISIBLE
                    }
                } else if (newText == "广东") {
                    mBinding.apply {
                        cardViewHot.visibility = View.GONE
                        llNone.visibility = View.GONE
                        recyclerSearch.visibility = View.VISIBLE
                        initSearchList()
                    }
                } else {
                    mBinding.apply {
                        cardViewHot.visibility = View.VISIBLE
                        recyclerSearch.visibility=View.GONE
                        llNone.visibility = View.GONE
                    }
                }
                return false
            }
        })

        //搜索按钮
        mBinding.tvSearch.setOnClickListener {

        }
    }

    private fun initSearchList() {
        mBinding.recyclerSearch.apply {
            layoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter = HotAdapter(null, 1)
        }
    }

    private fun initHotList() {
        //网络热门加载数据
        RetrofitClient.create(ApiService::class.java).getHot()
            .enqueue(object : retrofit2.Callback<Hot> {
                override fun onResponse(call: Call<Hot>, response: Response<Hot>) {
                    Log.e("hgm", "热门列表请求成功: ${response.body().toString()}")

                    //获取HotData类型的数据集合
                    val hotList = response.body()?.data
                    mBinding.recyclerView.apply {
                        layoutManager =
                            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
                        adapter = hotList?.let { HotAdapter(it, 0) }
                    }
                }

                override fun onFailure(call: Call<Hot>, t: Throwable) {
                    Log.e("hgm", "热门列表请求失败: ${t.message}")
                }
            })


    }

    //Adapter
    inner class HotAdapter(private var hotList: List<HotData>?, var type: Int) :
        RecyclerView.Adapter<HotAdapter.MyViewHolder>() {

        inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val item_head = view.findViewById<ImageView>(R.id.item_head)
            val item_content = view.findViewById<TextView>(R.id.item_content)

            var item_head2 = view.findViewById<ImageView>(R.id.item_head)
            var item_title = view.findViewById<TextView>(R.id.item_title)
            var item_content2 = view.findViewById<TextView>(R.id.item_content)
            var item_price = view.findViewById<TextView>(R.id.item_price)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            return if (viewType == type) {
                val view =
                    LayoutInflater.from(parent.context).inflate(R.layout.hot_item, parent, false)
                MyViewHolder(view)
            } else {
                val view =
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_gridlayout, parent, false)
                MyViewHolder(view)
            }

        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            // holder.item_head.setImageResource(R.drawable.hot_bg)
            // holder.item_content.text="广州一日游"

            // val hotData = hotList[position]
            // Glide.with(this@SearchActivity).load(hotData.imgUrl).into(holder.item_head)
            // holder.item_content.text = hotData.title

            when (type) {
                0 -> {
                    val hotData = hotList?.get(position)
                    Glide.with(this@SearchActivity).load(hotData?.imgUrl).into(holder.item_head)
                    holder.item_content.text = hotData?.title
                }
                1 -> {
                    if (position % 2 == 0) {
                        holder.item_head2.setImageResource(R.drawable.bg1)
                        holder.item_title.text = "避暑山庄 冷泉滩"
                        holder.item_price.text = "￥60起"
                    } else {
                        holder.item_head2.setImageResource(R.drawable.bg2)
                        holder.item_title.text = "小众海湾"
                        holder.item_price.text = "￥388起"
                    }
                    holder.item_content2.isSingleLine =
                        (position == 0 || position == 3 || position == 4 || position == 7 || position == 9)
                    holder.item_content2.text = "经过千万年的地层变化，海风侵蚀形成了令人叹为观止，值得到此一游"
                }
            }
        }

        override fun getItemCount(): Int {
            return if (type == 0) { hotList!!.size } else { 20 }
        }

    }


}